package fr.channel.client.metier.impl;

import com.google.gwt.appengine.channel.client.Channel;
import com.google.gwt.appengine.channel.client.ChannelFactory;
import com.google.gwt.appengine.channel.client.SocketError;
import com.google.gwt.appengine.channel.client.SocketListener;
import com.google.web.bindery.event.shared.EventBus;

import fr.channel.client.ChannelTest;
import fr.channel.client.event.MessageEvent;
import fr.channel.client.metier.SmChannel;

public class SmChannelImpl implements SmChannel {
	public void createChannel(String token, final EventBus eventBus) {
		ChannelFactory.createChannel(token, new ChannelFactory.ChannelCreatedCallback() {
			
			@Override
			public void onChannelCreated(Channel channel) {
				channel.open(new SocketListener() {

					@Override
					public void onOpen() {
						ChannelTest.log("Channel opened!");
					}

					@Override
					public void onMessage(String message) {
						ChannelTest.log("Received: " + message);
						eventBus.fireEvent(new MessageEvent(message));
					}

					@Override
					public void onError(SocketError error) {
						ChannelTest.log("Error: " + error.getDescription());

					}

					@Override
					public void onClose() {
						ChannelTest.log("Channel closed!");

					}
				});
				
			}
		});

	}
}


